package com.vehicle.controller;

import com.vehicle.bean.DataGridView;
import com.vehicle.bean.ResultDto;
import com.vehicle.pojo.AccidentRecord;
import com.vehicle.pojo.Car;
import com.vehicle.service.AccidentRecordService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/accidentRecord")
public class AccidentRecordController extends BasePreciseController{
    private Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private AccidentRecordService accidentRecordService;
    @ResponseBody
    @RequestMapping("/accidentRecords")
    public DataGridView accidentRecordList(AccidentRecord accidentRecord,Integer carid){
        Car car = new Car();
        car.setCarid(carid);
        accidentRecord.setCars(car);
        return accidentRecordService.findAccidentRecordPage(accidentRecord);
    }
    @ResponseBody
    @RequestMapping(value = "/deleteAccidentRecord")
    public ResultDto deleteAccidentRecord(Integer accidentid){
        ResultDto resultDto = new ResultDto();
        try {
            accidentRecordService.deleteAccident(accidentid);
            resultDto.setMsg("删除成功");
            resultDto.setCode(200);
        } catch (Exception e) {
            resultDto.setCode(-200);
            resultDto.setMsg("删除失败");
        }
        return resultDto;
    }
    @RequestMapping(value = "/addAccidentRecord")
    @ResponseBody
    public ResultDto addAccidentRecord(AccidentRecord accidentRecord){
        ResultDto resultDto = new ResultDto();
        try {
            accidentRecordService.addAccidentRecord(accidentRecord);
            resultDto.setCode(200);
            resultDto.setMsg("增加成功");
        } catch (Exception e) {
            resultDto.setCode(-200);
            resultDto.setMsg("增加失败");
            e.printStackTrace();
        }
        return resultDto;
    }
    @ResponseBody
    @RequestMapping(value = "/updateAccidentRecord")
    public ResultDto updateAccidentRecord(AccidentRecord accidentRecord){
        ResultDto resultDto  = new ResultDto();
//        logger.info(accidentRecord);
        try {
            accidentRecordService.updateAccidentRecord(accidentRecord);
            resultDto.setMsg("修改成功");
            resultDto.setCode(200);
        } catch (Exception e) {
            resultDto.setMsg("修改失败");
            resultDto.setCode(-200);
            e.printStackTrace();
        }
        return resultDto;
    }
    @ResponseBody
    @RequestMapping(value = "/deleteBatchAccidentRecord")
    public ResultDto deleteBatchAccidentRecord(@RequestParam("accidentIds[]") Integer []accidentIds){
        ResultDto resultDto = new ResultDto();
        try {
            accidentRecordService.deleteBatchAccidentRecord(accidentIds);
            resultDto.setMsg("删除成功");
            resultDto.setCode(200);
        } catch (Exception e) {
            resultDto.setMsg("删除失败");
            resultDto.setCode(-200);
            e.printStackTrace();
        }finally {
            return resultDto;
        }
    }
}
